//
//  WBComposePictureView.swift
//  weibo
//
//  Created by 胡古古 on 2017/6/13.
//  Copyright © 2017年 hugugu. All rights reserved.
//

import UIKit

class WBComposePictureView: UICollectionView {
    
    lazy var imageArray:[UIImage] = [UIImage]()
    
    var addPictureCallBack:(()->())?
    // 初始化方法
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        let flowLayout = UICollectionViewFlowLayout()
        
        let itmeMargin: CGFloat = 5
        
        let itmeWith = (ScreenWidth - 2 * HomeCellMargin - 2 * itmeMargin) / 3
        
        flowLayout.itemSize = CGSize(width: itmeWith, height: itmeWith)
        
        flowLayout.minimumLineSpacing = itmeMargin
        
        flowLayout.minimumInteritemSpacing = itmeMargin
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        self.isHidden = true
            
        self.register(WBComposePictureViewCell.self, forCellWithReuseIdentifier: composeCell_id)
        
        dataSource = self
        
        delegate = self
    }
    
    func show(image:UIImage) {
        
        // 最多显示9张
        if imageArray.count == 9 {
            
            return
        }
        
        self.isHidden = false
        imageArray.append(image)
        
        reloadData()
        
    }
    
}

extension WBComposePictureView : UICollectionViewDataSource,UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        if  imageArray.count == 0 || imageArray.count == 9 {
            
            return imageArray.count
        }
        
        return imageArray.count + 1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: composeCell_id, for: indexPath) as! WBComposePictureViewCell
        
        if indexPath.item == imageArray.count {
            
            cell.image = nil
        } else {
            
            cell.image = imageArray[indexPath.item]
        }
        
        cell.indexPath = indexPath
        
        cell.deletePictureCallBack = { [unowned self] (index:Int)->() in
            
            self.imageArray.remove(at: index)
            
            self.reloadData()
            
            if self.imageArray.count == 0 {
                
                self.isHidden = true
            }
        }
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        if indexPath.item == imageArray.count {

            addPictureCallBack?()
        }
    }
    
}

class WBComposePictureViewCell: UICollectionViewCell {
    
    var deletePictureCallBack:((Int)->())?
    
    var indexPath:IndexPath?
    
    fileprivate lazy var imageView: UIImageView = UIImageView()
    
    fileprivate lazy var deleteButton: UIButton = {
       
        let button = UIButton()
        button.addTarget(self, action: #selector(clickButton), for: .touchUpInside)
        button.setImage(UIImage(named:"compose_photo_close"), for: .normal)
        
        return button
    }()
    
    var image: UIImage? {
        
        didSet{
            if image == nil {
                
                imageView.image = UIImage(named: "compose_pic_add")
                
                deleteButton.isHidden = true
            } else {
                
                imageView.image = image
                
                deleteButton.isHidden = false
            }
        }
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        contentView.addSubview(imageView)
        contentView.addSubview(deleteButton)
        
        imageView.snp.makeConstraints { (make) in
            
            make.edges.equalTo(contentView)
        }
        
        deleteButton.snp.makeConstraints { (make) in
            
            make.top.equalTo(imageView)
            make.right.equalTo(imageView)
        }
        
    }
    
    @objc private func clickButton() {
        
        if let currentIndexPath = indexPath {
            
            let itme = currentIndexPath.item
            
            deletePictureCallBack?(itme)
        }
        
    }
    
    
}
